package com.ihighest.app.infox.activity;

import org.json.JSONException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.base.AbstractActivity;
import com.ihighest.app.common.core.Application;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.activity.InformationActivity.ChangeBroadcast;
import com.ihighest.app.infox.myview.ActivityTitle;
import com.ihighest.app.infox.myview.InvitedFriendDialog;
import com.ihighest.app.infox.myview.SettingImgView;
import com.readystatesoftware.viewbadger.BadgeView;

public class FindActivity extends AbstractActivity implements OnClickListener {
	private ActivityTitle title;
	private SettingImgView findNearStoresSettingImageView;
	private SettingImgView joinInSettingImageView;
	private SettingImgView plantSettingImageView;
	private SettingImgView rankingTodaySettingImageView;

	private SettingImgView surveySettingImgView;
	private SettingImgView voteSettingImgView;
	private ChangeBroadcast changeBroadcast;
	private BadgeView badge1;
	private BadgeView badge2;
	private BadgeView badge3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Application.getInstance().addActivity(this);
		setContentView(R.layout.infox_activity_find);
		title = (ActivityTitle) findViewById(R.id.infox_findTitle);
		title.setTitle("发现");
		title.setViewVersible(title.titleMsg);
		title.setViewGone(title.rightLayout, title.titleSetting, title.titleRefresh);

		changeBroadcast = new ChangeBroadcast();
		IntentFilter filter = new IntentFilter();
		filter.addAction(Const.CHANGECOUNT);
		this.registerReceiver(changeBroadcast, filter);
		init();
		// loadData();
	}

	private void init() {
		findNearStoresSettingImageView = (SettingImgView) findViewById(R.id.id_findnearstores_settingImgview);
		findNearStoresSettingImageView.setOnClickListener(this);
		joinInSettingImageView = (SettingImgView) findViewById(R.id.id_joinin_settingimageview);
		joinInSettingImageView.setOnClickListener(this);
		plantSettingImageView = (SettingImgView) findViewById(R.id.id_plant_settingview);
		plantSettingImageView.setOnClickListener(this);

		rankingTodaySettingImageView = (SettingImgView) findViewById(R.id.id_Ranktoday_settingimageview);
		rankingTodaySettingImageView.setOnClickListener(this);
		surveySettingImgView = (SettingImgView) findViewById(R.id.id_survey_settingImgview);
		surveySettingImgView.setOnClickListener(this);
		voteSettingImgView = (SettingImgView) findViewById(R.id.id_vote_settingImgview);
		voteSettingImgView.setOnClickListener(this);
		findViewById(R.id.id_event_settingImgview).setOnClickListener(this);
		findViewById(R.id.id_invitedFriend_settingview).setOnClickListener(this);
		findViewById(R.id.id_integral_gold_settingview).setOnClickListener(this);
		findViewById(R.id.id_havemoney_settingImgview).setOnClickListener(this);
		badge1 = new BadgeView(this, surveySettingImgView);
		badge2 = new BadgeView(this, voteSettingImgView);
		badge3 = new BadgeView(this, findViewById(R.id.id_event_settingImgview));

	}

	void loadData() {
		Api.get("me/findMeUnJoinInfo", ApiParam.Empty, new ApiCallback(this, true) {

			@Override
			public void handleSuccess(ApiResult result) {
				if (result.getCode() == 100) {
					try {
						if (result.getData().getInt("survey") > 0) {
							// BadgeView badge = new BadgeView(mContext,
							// surveySettingImgView);
							badge1.setText(result.getData().getInt("survey") > 9 ? "n" : result.getData().getInt("survey") + "");
							badge1.setTextSize(11);
							badge1.setGravity(Gravity.CENTER);
							badge1.setBackgroundResource(R.drawable.infox_orange_roungbg);
							badge1.setTextColor(Color.parseColor("#fa9600"));
							badge1.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
							badge1.show(true);
							badge1.setVisibility(View.VISIBLE);
						} else {
							badge1.setVisibility(View.GONE);
						}

						if (result.getData().getInt("vote") > 0) {

							badge2.setText(result.getData().getInt("vote") > 9 ? "n" : result.getData().getInt("vote") + "");
							badge2.setTextSize(11);
							badge2.setGravity(Gravity.CENTER);
							badge2.setBackgroundResource(R.drawable.infox_orange_roungbg);
							badge2.setTextColor(Color.parseColor("#fa9600"));
							badge2.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
							badge2.show(true);
							badge2.setVisibility(View.VISIBLE);
						} else {
							badge2.setVisibility(View.GONE);
						}
						if (result.getData().getInt("activity") > 0) {
							badge3.setText(result.getData().getInt("activity") > 9 ? "n" : result.getData().getInt("activity") + "");
							badge3.setTextSize(11);
							badge3.setGravity(Gravity.CENTER);
							badge3.setBackgroundResource(R.drawable.infox_orange_roungbg);
							badge3.setTextColor(Color.parseColor("#fa9600"));
							badge3.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
							badge3.show(true);
							badge3.setVisibility(View.VISIBLE);
						} else {
							badge3.setVisibility(View.GONE);
						}
					} catch (JSONException e) {
						// pass
					}
				}
			}

			@Override
			public void handleFailure(Exception e, String msg) {
			}
		}, this);
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.e("", "进F");
		loadData();

		if (Const.UNREADMESSAGE != 0) {
			title.messagenumber.setText(Const.UNREADMESSAGE + "");
			title.messagenumber.setVisibility(View.VISIBLE);
		} else {
			title.messagenumber.setVisibility(View.GONE);
		}

	}

	@Override
	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.id_findnearstores_settingImgview:
			ToastKit.showShort(this, Const.UN_FINISH_ALERT);
			// Intent findIntent = new Intent(this,
			// NearAriculturalStoresActivity.class);
			// startActivity(findIntent);
			break;
		case R.id.id_havemoney_settingImgview:
			ToastKit.showShort(this, Const.UN_FINISH_ALERT);
			// Intent findIntent = new Intent(this,
			// NearAriculturalStoresActivity.class);
			// startActivity(findIntent);
			break;
		case R.id.id_invitedFriend_settingview:
			if (!TextUtils.isEmpty(SharedPreferencesKit.getString(FindActivity.this, Const.SP_KEY_USER_ID))) {
				Api.get("share", ApiParam.create().addParam("type", "AppLoad"), new ApiCallback(FindActivity.this) {

					@Override
					public void handleFailure(Exception e, String msg) {
					}

					@Override
					public void handleSuccess(ApiResult result) {

						try {
							InvitedFriendDialog dialog = new InvitedFriendDialog(FindActivity.this, R.style.CustomDialog);

							dialog.setShareId(result.getData().getString("ShareId"));
							dialog.show();
						} catch (JSONException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}
				}, FindActivity.this);
			} else {
				ToastKit.showShort(FindActivity.this, Const.UN_LOGIN_ALERT);
			}

			break;

		case R.id.id_joinin_settingimageview:
			ToastKit.showShort(this, Const.UN_FINISH_ALERT);
			// Intent joinIntent = new Intent(this, JoinInStoresActivity.class);
			// startActivity(joinIntent);
			break;
		case R.id.id_plant_settingview:
			Intent plantIntent = new Intent(this, PlantEncyclopediaActivity.class);
			startActivity(plantIntent);
			break;
		case R.id.id_Ranktoday_settingimageview:
			Intent rankingIntent = new Intent(this, RankingTodayActivity.class);
			startActivity(rankingIntent);
			break;
		case R.id.id_survey_settingImgview:
			Intent surveyIntent = new Intent(this, QuestionnaireActivity.class);
			startActivity(surveyIntent);
			break;
		case R.id.id_vote_settingImgview:
			Intent voteIntent = new Intent(this, OnLineVoteActivity.class);
			startActivity(voteIntent);
			break;
		case R.id.id_event_settingImgview:
			Intent eventIntent = new Intent(this, EventActivity.class);
			startActivity(eventIntent);
			break;
		case R.id.id_integral_gold_settingview:
			Intent goldIntent = new Intent(this, AssignmentCenteringActivity.class);
			startActivity(goldIntent);
			break;

		}
	}

	public class ChangeBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// 得到数据变化的广播
			if (intent.getAction().equals(Const.CHANGECOUNT)) {
				if (Const.UNREADMESSAGE != 0) {
					title.messagenumber.setText(Const.UNREADMESSAGE + "");
					title.messagenumber.setVisibility(View.VISIBLE);
				} else {
					title.messagenumber.setVisibility(View.GONE);
				}

			}

		}

	}
}
